Option Strict On
Option Explicit On 

Imports System.Windows.Forms
Imports C1.Win.C1Input
Imports system.drawing
''' -----------------------------------------------------------------------------
''' Project	 : IPCommon
''' Class	 : Core.IPCommon.CErrorTextBoxHandler
''' 
''' -----------------------------------------------------------------------------
''' <summary>
'''     Display message for editing in text box
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	2/21/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CErrorTextBoxHandler

    Private Const C_ERROR_CONTROL_IMAGE_INDEX As Integer = 14
    Private C_BACKCOLOR_ERROR As system.Drawing.Color = system.DRAWING.Color.pink
    Protected WithEvents m_txtBox As TextBox
    Protected WithEvents m_form As Form
    Protected m_old_backcolor_of_txtBox As Drawing.Color

    Protected Sub New(ByVal i_txtBox As TextBox)
        m_txtBox = i_txtBox
        m_txtBox.Tag = Me
        m_form = i_txtBox.FindForm
        addErrorMark()
        Try
            m_txtBox.Focus()
            AddHandler m_txtBox.TextChanged, AddressOf handle_changed
            AddHandler m_txtBox.Leave, AddressOf handle_leave
        Catch v_e As Exception
            AddHandler m_form.KeyDown, AddressOf keydown_in_form
        End Try
    End Sub

    Private Sub removeErrorMarkandHandlers()
        removeErrorMark()
        RemoveHandler m_txtBox.TextChanged, AddressOf handle_changed
        RemoveHandler m_txtBox.Leave, AddressOf handle_leave
        RemoveHandler m_form.KeyDown, AddressOf keydown_in_form
        m_txtBox.Tag = Nothing
    End Sub

    Private Sub handle_changed(ByVal sender As Object, ByVal e As System.EventArgs)
        removeErrorMarkandHandlers()
    End Sub

    Private Sub handle_leave(ByVal sender As Object, ByVal e As System.EventArgs)
        removeErrorMarkandHandlers()
    End Sub

    Protected Overridable Sub keydown_in_form(ByVal sender As Object, ByVal e As KeyEventArgs)
        removeErrorMarkandHandlers()
    End Sub


    Private Sub addErrorMark()
        m_old_backcolor_of_txtBox = m_txtBox.BackColor
        m_txtBox.BackColor = Me.C_BACKCOLOR_ERROR
    End Sub

    Private Sub removeErrorMark()
        m_txtBox.BackColor = m_old_backcolor_of_txtBox
    End Sub


    Private Shared Function getPictureBox(ByVal i_height As Integer, ByVal i_width As Integer) As PictureBox
        Dim v_pictureBox As New PictureBox
        v_pictureBox.Image = IPImages.getImageResource(C_ERROR_CONTROL_IMAGE_INDEX)
        v_pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        v_pictureBox.Size = New Size(i_width, i_height)
        Return v_pictureBox
    End Function

    Private Sub addPB(ByVal i_picturebox As PictureBox, ByVal i_top As Integer, ByVal i_left As Integer)
        i_picturebox.Top = i_top
        i_picturebox.Left = i_left
        i_picturebox.BringToFront()
        m_txtBox.FindForm.Controls.Add(i_picturebox)
    End Sub

    Public Shared Sub markAsErrorTxtBox(ByVal i_txtBox As TextBox)
        Dim v_err As New CErrorTextBoxHandler(i_txtBox)
    End Sub
    
End Class

''' -----------------------------------------------------------------------------
''' Project	 : IPCommon
''' Class	 : Core.IPCommon.CErrorTextBoxHandlerStatusBar
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Cho phép xử lý các lỗi trên textbox , hiện thị message ra status bar
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	2/22/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CErrorTextBoxHandlerStatusBar
    Inherits CErrorTextBoxHandler

    Protected m_message_handler As IPStatusBarMessages

    Protected Sub New(ByVal i_txtBox As TextBox, ByVal i_msg_handler As IPStatusBarMessages)
        MyBase.New(i_txtBox)
        m_message_handler = i_msg_handler
    End Sub
    Protected Sub New(ByVal i_txtBox As C1TextBox, ByVal i_msg_handler As IPStatusBarMessages)
        MyBase.New(i_txtBox)
        m_message_handler = i_msg_handler
    End Sub

    Protected Overrides Sub keydown_in_form(ByVal sender As Object, ByVal e As KeyEventArgs)
        m_message_handler.display_message("")
        MyBase.keydown_in_form(sender, e)
    End Sub

    Public Shared Sub markAsErrorTxtBoxStatusBar(ByVal i_txtBox As TextBox, _
                                                 ByVal i_msg_handler As IPStatusBarMessages)
        Dim v_err As New CErrorTextBoxHandlerStatusBar(i_txtBox, i_msg_handler)
    End Sub
    Public Shared Sub markAsErrorC1TxtBoxStatusBar(ByVal i_txtBox As C1TextBox, _
                                                ByVal i_msg_handler As IPStatusBarMessages)
        Dim v_err As New CErrorTextBoxHandlerStatusBar(i_txtBox, i_msg_handler)
    End Sub
End Class
